/*
 * Copyright (c) 2010, Nikolaus Moll
 * All rights reserved.
 */
package net.miblounge.gradecalculator.gui.dialogs;

import java.util.List;
import java.util.Vector;

import net.miblounge.gradecalculator.core.AdaptedRegulations;
import net.miblounge.gradecalculator.core.descriptors.courses.CourseDescription;
import net.miblounge.gradecalculator.core.descriptors.modulelistitems.IndividualModule;
import net.miblounge.gradecalculator.guicommon.interfaces.IValidationListener;
import net.miblounge.gradecalculator.guicommon.layouts.VerySimpleLayout;
import net.miblounge.gradecalculator.guicommon.widgets.CourseEditor;
import net.miblounge.miglayout.qt.MigLayout;

import com.trolltech.qt.gui.QGroupBox;
import com.trolltech.qt.gui.QWidget;

public class IndividualModuleResultDialog extends AbstractResultDialog implements IValidationListener
{
    private CourseEditor courseEditor;
    private final IndividualModule module;

    public IndividualModuleResultDialog(final QWidget parent, final AdaptedRegulations regulations,
            final CourseDescription course)
    {
        super(parent, regulations, course);
        this.module = course.getParentModule();

        revalidate();
    }

    @Override
    protected List<QWidget> createAdditonalWidgets(final QWidget parent)
    {
        final Vector<QWidget> result = new Vector<QWidget>();
        result.add(createCourseEditor(parent));
        return result;
    }

    protected QGroupBox createCourseEditor(final QWidget parent)
    {
        final QGroupBox result = new QGroupBox(tr("Course"), parent);
        final VerySimpleLayout outerLayout = new VerySimpleLayout(result);
        final QWidget container = new QWidget(result);
        outerLayout.setWidget(container);

        final MigLayout innerLayout = new MigLayout("", "[][grow,fill]");
        container.setLayout(innerLayout);

        courseEditor = new CourseEditor(container, getCourse());
        innerLayout.addWidget(courseEditor, "grow");
        result.setLayout(outerLayout);
        return result;
    }

    @Override
    protected boolean hasGradedPerformance()
    {
        if (courseEditor == null) {
            return true;
        }

        // TODO
        // return courseEditor.getGradedPerformance() != ExaminationType.NONE;
        return true;
    }

    @Override
    protected boolean hasUngradedPerformance()
    {
        if (courseEditor == null) {
            return true;
        }

        // TODO
        // return courseEditor.getUngradedPerformance() != ExaminationType.NONE;
        return true;
    }

    @Override
    protected boolean hasEstimatedGroup()
    {
        return true;
    }

    @Override
    protected String getCourseName()
    {
        return "Indivual Course";
    }

    @Override
    protected CourseDescription getCourse()
    {
        return (CourseDescription) super.getCourse();
    }

    @Override
    public void revalidate()
    {
        this.setGradedPerformanceEnabled(!courseEditor.getGradedPerformance().isEmpty());
        this.setUngradedPerformanceEnabled(!courseEditor.getUngradedPerformance().isEmpty());
        super.revalidate();
    }

    @Override
    public void apply()
    {
        courseEditor.apply();

        getCourse().setModuleName(module.getName());
        getResult().setCourseIdentifier(module.getIdentifier() + "." + getCourse().getName());

        if (!getCourse().getGradedPerformance().isEmpty()) {
            getResult().setGrades(this.getGrades());
        }
        if (!getCourse().getUngradedPerformance().isEmpty()) {
            getResult().setUngradedPassed(this.getUngradedPassed());
        }

        super.apply();
    }

    @Override
    protected boolean validate()
    {
        if (!courseEditor.validate()) {
            return false;
        }
        return super.validate();
    }

}
